package com.otitan.forest.right.controller;

import com.otitan.forest.right.dto.StatisticsDto;
import com.otitan.forest.right.entity.BssAuditRecordsEntity;
import com.otitan.forest.right.entity.BssForestRightInfoEntity;
import com.otitan.forest.right.service.BssForestRightInfoService;
import com.otitan.forest.right.util.CommonUtil;
import com.otitan.forest.right.util.FileUtil;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 林权证登记
 *
 * @author xiazw
 * @email *****@mail.com
 * @date 2020-08-04 14:19:43
 */
@Api(tags = "林权证登记API")
@RestController
@RequestMapping("/bssForestRightInfo")
public class BssForestRightInfoController {

    @Autowired
    private BssForestRightInfoService bssForestRightInfoService;

//    @Autowired
//    private BssForestRightInfoProducers producers;

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public DataResult add(@RequestBody BssForestRightInfoEntity bssForestRightInfo) {
        return bssForestRightInfoService.add(bssForestRightInfo);

    }

    @RequiresRoles(value = {"admin", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "处理林权")
    @PostMapping("/dealWith")
    public DataResult dealWith(@RequestBody BssAuditRecordsEntity entity) {
        return bssForestRightInfoService.dealWith(entity);
    }

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        bssForestRightInfoService.removeByIds(ids);
        // 根据 ids 查询数据
//        List<BssForestRightInfoEntity> list = bssForestRightInfoService.findItemsByIds(ids);
//        for (BssForestRightInfoEntity entity : list) {
//            // 加入队列
//            producers.send(JSONObject.toJSONString(entity));
//        }
        return DataResult.success();
    }

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public DataResult update(@RequestBody BssForestRightInfoEntity bssForestRightInfo) {
        return bssForestRightInfoService.changeData(bssForestRightInfo);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    public DataResult findListByPage(@RequestBody BssForestRightInfoEntity entity) {
        return bssForestRightInfoService.findListByPage(entity);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "通过id获取处理记录")
    @PostMapping("/findRecordsById/{id}")
    public DataResult findRecordsById(@PathVariable String id) {
        List<BssAuditRecordsEntity> bssAuditRecordsEntityList = bssForestRightInfoService.findRecordsById(id);
        return DataResult.success(bssAuditRecordsEntityList);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "根据Id查询实体")
    @GetMapping("/findOne/{id}")
    public DataResult findOneById(@PathVariable String id) {
        BssForestRightInfoEntity entityObj = bssForestRightInfoService.getById(id);
        return DataResult.success(entityObj);
    }

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "批量导入")
    @PostMapping("/batchImport")
    public DataResult batchImportFile(@RequestParam("file") MultipartFile file) {
        return bssForestRightInfoService.batchImportFile(file);
    }


    @RequiresRoles(value = {"admin", "audit", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "进度情况统计接口")
    @PostMapping("/progressStatistics")
    public DataResult progressStatistics(@RequestBody StatisticsDto dto) {
        return bssForestRightInfoService.progressStatistics(dto);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "导出接口")
    @PostMapping("/download")
    public void download(@RequestBody BssForestRightInfoEntity entity, HttpServletResponse httpServletResponse) throws IOException {
        bssForestRightInfoService.download(entity, httpServletResponse);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "导出压缩包")
    @PostMapping("/exportZip")
    public void exportZip(@RequestBody BssForestRightInfoEntity entity, HttpServletResponse httpServletResponse) throws IOException {
        bssForestRightInfoService.exportZip(entity, httpServletResponse);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "下载文件")
    @PostMapping("/exportMdb")
    public void exportMdb(HttpServletResponse response, BssForestRightInfoEntity bssForestRightInfoEntity) {
        bssForestRightInfoService.exportMdb(response, bssForestRightInfoEntity);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadMdb")
    public DataResult uploadMdb(@RequestParam("file") MultipartFile file) {
        return bssForestRightInfoService.uploadMdb(file);
    }

}
